package com.takepoints.mobile.zxing;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.google.zxing.ResultMetadataType;
import com.google.zxing.ResultPoint;
import com.takepoints.mobile.BaseActivity;
import com.takepoints.mobile.MyCardDetail;
import com.takepoints.mobile.MyOfferDetail;
import com.takepoints.mobile.MyRewardDetail;
import com.takepoints.mobile.R;
import com.takepoints.mobile.Takepoints;
import com.takepoints.mobile.TPPromotions;

import com.takepoints.android.TPConst;
import com.takepoints.android.bo.Promotion;
import com.takepoints.android.services.PublicServices;
import com.takepoints.android.services.TPServices;
import com.takepoints.android.vo.ReturnVal;
import com.takepoints.android.vo.TakeReturn;
import com.takepoints.mobile.zxing.camera.CameraManager;
import com.takepoints.android.util.SecurityUtil;
import com.takepoints.android.util.StringUtil;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.ClipboardManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
	http://stackoverflow.com/questions/10184511/how-to-import-the-zxing-library-for-my-app-for-encoding-qr-codeswhile-there-is
	http://stackoverflow.com/questions/5969679/yet-another-question-about-embedding-zxing-in-android-app
	http://stackoverflow.com/questions/6494683/how-to-write-a-barcode-scanner-in-android-using-zxing
	http://stackoverflow.com/questions/9539351/switching-zxing-barcode-scanner-to-portrait-mode
	http://stackoverflow.com/questions/4782543/integration-zxing-library-directly-into-my-android-application
	http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/
*/
public final class CaptureActivity extends BaseActivity implements SurfaceHolder.Callback {
	
	private static final String TAG = CaptureActivity.class.getSimpleName();
	
	private static final int SHARE_ID = Menu.FIRST;
	private static final int HISTORY_ID = Menu.FIRST + 1;
	private static final int SETTINGS_ID = Menu.FIRST + 2;
	private static final int HELP_ID = Menu.FIRST + 3;
	private static final int ABOUT_ID = Menu.FIRST + 4;

	private static final long DEFAULT_INTENT_RESULT_DURATION_MS = 1500L;
	private static final long BULK_MODE_SCAN_DELAY_MS = 1000L;

	private static final String PACKAGE_NAME = "com.takepoints.mobile.zxing";
	private static final String PRODUCT_SEARCH_URL_PREFIX = "http://www.google";
	private static final String PRODUCT_SEARCH_URL_SUFFIX = "/m/products/scan";
	private static final String[] ZXING_URLS = { "http://zxing.appspot.com/scan", "zxing://scan/" };
	private static final String RETURN_CODE_PLACEHOLDER = "{CODE}";
	private static final String RETURN_URL_PARAM = "ret";
	private static final String RAW_PARAM = "raw";

	public static final int HISTORY_REQUEST_CODE = 0x0000bacc;
	
	private CameraManager cameraManager;
	private CaptureActivityHandler handler;
	private Result savedResultToShow;
	private ViewfinderView viewfinderView;
	private TextView statusView;
	private View resultView;
	private Result lastResult;
	private boolean hasSurface;
	private String sourceUrl;
	private String returnUrlTemplate;
	private boolean returnRaw;
	private Collection<BarcodeFormat> decodeFormats;
	private String characterSet;
	private String versionName;

	private int cardId;
	private String pageSrc;
	private String promotionId;
	private String rewardNum;

	private final DialogInterface.OnClickListener aboutListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialogInterface, int i) {
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri
					.parse(getString(R.string.zxing_url)));
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
			startActivity(intent);
		}
	};

	ViewfinderView getViewfinderView() {
		return viewfinderView;
	}
	public Handler getHandler() {
		return handler;
	}
	CameraManager getCameraManager() {
		return cameraManager;
	}

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.capture);
		
		Bundle bundle = this.getIntent().getExtras();
        pageSrc = bundle.getString("pageSrc");
        
		if( !"TakeNow".equalsIgnoreCase(pageSrc) ){
			cardId = Integer.parseInt(bundle.getString("cardId"));
		}
        if("MyCardRewardDetail".equalsIgnoreCase(pageSrc)){
        	promotionId = bundle.getString("promotionId");
        	rewardNum = bundle.getString("rewardNum");
        }
		hasSurface = false;
	}

	@Override
	protected void onResume() {
		super.onResume();

		// CameraManager must be initialized here, not in onCreate(). This is
		// necessary because we don't
		// want to open the camera driver and measure the screen size if we're
		// going to show the help on
		// first launch. That led to bugs where the scanning rectangle was the
		// wrong size and partially
		// off screen.
		cameraManager = new CameraManager(getApplication());

		viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
		viewfinderView.setCameraManager(cameraManager);

		resultView = findViewById(R.id.result_view);
		statusView = (TextView) findViewById(R.id.status_view);

		handler = null;
		lastResult = null;

		resetStatusView();

		SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
		SurfaceHolder surfaceHolder = surfaceView.getHolder();
		if (hasSurface) {
			// The activity was paused but not stopped, so the surface still exists.
			// Therefore surfaceCreated() won't be called, so init the camera here.
			initCamera(surfaceHolder);
		} else {
			// Install the callback and wait for surfaceCreated() to init the camera.
			surfaceHolder.addCallback(this);
			surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		}

		Intent intent = getIntent();

		decodeFormats = null;
		characterSet = null;
		
		if(intent != null){
			String action = intent.getAction();
			String dataString = intent.getDataString();
//popup("action = " + action + "\n dataString = " + dataString);
			if (Intents.Scan.ACTION.equals(action)){
				// Scan the formats the intent requested, and return the result
				// to the calling activity.
				decodeFormats = DecodeFormatManager.parseDecodeFormats(intent);

				if (intent.hasExtra(Intents.Scan.WIDTH)
						&& intent.hasExtra(Intents.Scan.HEIGHT)) {
					int width = intent.getIntExtra(Intents.Scan.WIDTH, 0);
					int height = intent.getIntExtra(Intents.Scan.HEIGHT, 0);
					if (width > 0 && height > 0) {
						cameraManager.setManualFramingRect(width, height);
					}
				}

				String customPromptMessage = intent.getStringExtra(Intents.Scan.PROMPT_MESSAGE);
				if (customPromptMessage != null) {
					statusView.setText(customPromptMessage);
				}

			} else if (dataString != null
					&& dataString.contains(PRODUCT_SEARCH_URL_PREFIX)
					&& dataString.contains(PRODUCT_SEARCH_URL_SUFFIX)) {

				sourceUrl = dataString;
				decodeFormats = DecodeFormatManager.PRODUCT_FORMATS;

			} else if (isZXingURL(dataString)) {

				// Scan formats requested in query string (all formats if none
				// specified).
				// If a return URL is specified, send the results there.
				// Otherwise, handle it ourselves.
				sourceUrl = dataString;
				Uri inputUri = Uri.parse(sourceUrl);
				returnUrlTemplate = inputUri
						.getQueryParameter(RETURN_URL_PARAM);
				returnRaw = inputUri.getQueryParameter(RAW_PARAM) != null;
				decodeFormats = DecodeFormatManager
						.parseDecodeFormats(inputUri);

			}

			characterSet = intent.getStringExtra(Intents.Scan.CHARACTER_SET);
//popup("characterSet = " + characterSet);
		}
	}

	private static boolean isZXingURL(String dataString) {
		if (dataString == null) {
			return false;
		}
		for (String url : ZXING_URLS) {
			if (dataString.startsWith(url)) {
				return true;
			}
		}
		return false;
	}

	@Override
	protected void onPause() {
		if (handler != null) {
			handler.quitSynchronously();
			handler = null;
		}
		cameraManager.closeDriver();
		if (!hasSurface) {
			SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
			SurfaceHolder surfaceHolder = surfaceView.getHolder();
			surfaceHolder.removeCallback(this);
		}
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_FOCUS
				|| keyCode == KeyEvent.KEYCODE_CAMERA) {
			// Handle these events so they don't launch the Camera app
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
/*
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		return true;
	}

	// Don't display the share menu item if the result overlay is showing.
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		menu.findItem(SHARE_ID).setVisible(lastResult == null);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		/ * switch(item.getItemId()){
			default: return super.onOptionsItemSelected(item);
		} * /
		return true;
	}
*/
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == RESULT_OK) {
			if (requestCode == HISTORY_REQUEST_CODE) {
				int itemNumber = intent.getIntExtra(Intents.History.ITEM_NUMBER, -1);
				if (itemNumber >= 0) {
					popup("onActivityResult = " + itemNumber);
				}
			}
		}
	}

	private void decodeOrStoreSavedBitmap(Bitmap bitmap, Result result) {
		// Bitmap isn't used yet -- will be used soon
		if (handler == null) {
			savedResultToShow = result;
		} else {
			if (result != null) {
				savedResultToShow = result;
			}
			if (savedResultToShow != null) {
				Message message = Message.obtain(handler,
						R.id.decode_succeeded, savedResultToShow);
				handler.sendMessage(message);
			}
			savedResultToShow = null;
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder){
		if(holder == null){
			//Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!");
			popup("*** WARNING *** surfaceCreated() gave us a null surface!");
		}
		if(!hasSurface){
			hasSurface = true;
			initCamera(holder);
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder){
		hasSurface = false;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
		
	}

	/**
	 * A valid barcode has been found, so give an indication of success and show
	 * the results.
	 * 
	 * @param rawResult
	 *            The contents of the barcode.
	 * @param barcode
	 *            A greyscale bitmap of the camera data which was decoded.
	 */
	public void handleDecode(Result rawResult, Bitmap barcode) {
		if(rawResult != null){
			try{
				String nfcId = SecurityUtil.decrypt(rawResult.getText(), SecurityUtil.KEY_TYPE_QR);
				if( "TakeNow".equalsIgnoreCase(pageSrc) ){
					new TakeNowProcess(this).execute(nfcId);
					
				} else{
					new TaskProcess(this).execute(nfcId);
				}
				/*ReturnVal retVal = null;
				 if("MyCardRewardDetail".equalsIgnoreCase(pageSrc)){
					 retVal = TPServices.rewardMyCard( getSharedIntValue(TPConst.PRF_MEMBER_ID)
	 												 , cardId, nfcId, rewardNum);
				 } else if("MyCardDetail".equalsIgnoreCase(pageSrc)){
					 retVal = TPServices.useMyCard( getSharedIntValue(TPConst.PRF_MEMBER_ID)
							 					  , cardId, "MANUAL", nfcId);
				 } else if("MyOfferDetail".equalsIgnoreCase(pageSrc)){
					 retVal = TPServices.useMyCoupon( getSharedIntValue(TPConst.PRF_MEMBER_ID)
		 					  					  , cardId, "MANUAL", nfcId);
				 } else{
					 throw new Exception("ไม่สามารถทำรายการได้");
				 }
				 if( !"MyCardDetail".equalsIgnoreCase(pageSrc) ){
						 if(retVal != null && !StringUtil.isEmpty(retVal.getTrxCode())){
							 alertResult( "กรุณาแสดงรหัสนี้แก่พนักงาน : " + retVal.getTrxCode()
									   + "\n\n(" + retVal.getTrxDesc() + ")");
						 } else{
							 alertResult("เกิดข้อผิดพลาด (" + retVal.getError() + ") :\n\n" + retVal.getMessage());
						 }
				 }*/
			 } catch(Exception e){
				 alertResult("เกิดข้อผิดพลาดทางเทคนิค :\n\n" + e.getMessage());
			 }
		} else{
			popup("ขออภัยค่ะ .. เกิดข้อผิดพลาดทางเทคนิค");
		}
		
		lastResult = rawResult;
		if(barcode != null){
			drawResultPoints(barcode, rawResult);
		}
	}
	
	class TaskProcess extends AsyncTask<String, Void, ReturnVal>{
		
		private Context context = null;
		private ProgressDialog progressDlg = null;
		
		TaskProcess(Context context){
			this.context = context;
		}
		
		@Override
        protected void onPreExecute(){
            super.onPreExecute();
            progressDlg = ProgressDialog.show(context, "Takepoints.com", "Taking ...", true);
		}
		
		@Override
        protected ReturnVal doInBackground(String... params){
			String nfcId = params[0];
			if("MyCardRewardDetail".equalsIgnoreCase(pageSrc)){
				 return TPServices.rewardMyCard( getSharedIntValue(TPConst.PRF_MEMBER_ID)
												 , cardId, nfcId, rewardNum);
				 
			 } else if("MyCardDetail".equalsIgnoreCase(pageSrc)){
				 return TPServices.useMyCard( getSharedIntValue(TPConst.PRF_MEMBER_ID)
																			   , cardId, "MANUAL", nfcId);
				 
			 } else if("MyOfferDetail".equalsIgnoreCase(pageSrc)){
				 return TPServices.useMyCoupon( getSharedIntValue(TPConst.PRF_MEMBER_ID)
	  					  															, cardId, "MANUAL", nfcId);
				 
			} else{
				return null;
			}
		}
	
		@Override
	    protected void onPostExecute(ReturnVal retVal){
	    	progressDlg.dismiss();
			if(retVal != null){
				if(!StringUtil.isEmpty(retVal.getTrxCode())){
					 alertResult( "กรุณาแสดงรหัสนี้แก่พนักงาน : " + retVal.getTrxCode()
							   			 + "\n\n(" + retVal.getTrxDesc() + ")");
				} else{
					alertResult("เกิดข้อผิดพลาด (" + retVal.getError() + ") :\n\n" + retVal.getMessage());
				}
			 } else{
				 alertResult("เกิดข้อผิดพลาด ไม่สามารถทำรายการได้ !");
			 }
		}
	}
	
	/**
	 * Superimpose a line for 1D or dots for 2D to highlight the key features of
	 * the barcode.
	 * 
	 * @param barcode A bitmap of the captured image.
	 * @param rawResult The decoded results which contains the points to draw.
	 */
	private void drawResultPoints(Bitmap barcode, Result rawResult){
		ResultPoint[] points = rawResult.getResultPoints();
		if (points != null && points.length > 0){
			Canvas canvas = new Canvas(barcode);
			Paint paint = new Paint();
			paint.setColor(getResources().getColor(R.color.result_image_border));
			paint.setStrokeWidth(3.0f);
			paint.setStyle(Paint.Style.STROKE);
			Rect border = new Rect(2, 2, barcode.getWidth() - 2, barcode
					.getHeight() - 2);
			canvas.drawRect(border, paint);

			paint.setColor(getResources().getColor(R.color.result_points));
			if (points.length == 2) {
				paint.setStrokeWidth(4.0f);
				drawLine(canvas, paint, points[0], points[1]);
			} else if (points.length == 4
					&& (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A || rawResult
							.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
				// Hacky special case -- draw two lines, for the barcode and
				// metadata
				drawLine(canvas, paint, points[0], points[1]);
				drawLine(canvas, paint, points[2], points[3]);
			} else {
				paint.setStrokeWidth(10.0f);
				for (ResultPoint point : points) {
					canvas.drawPoint(point.getX(), point.getY(), paint);
				}
			}
		}
	}

	private static void drawLine(Canvas canvas, Paint paint, ResultPoint a,
			ResultPoint b) {
		canvas.drawLine(a.getX(), a.getY(), b.getX(), b.getY(), paint);
	}
	
	private void sendReplyMessage(int id, Object arg) {
		Message message = Message.obtain(handler, id, arg);
		long resultDurationMS = getIntent().getLongExtra(
				Intents.Scan.RESULT_DISPLAY_DURATION_MS,
				DEFAULT_INTENT_RESULT_DURATION_MS);
		if (resultDurationMS > 0L) {
			handler.sendMessageDelayed(message, resultDurationMS);
		} else {
			handler.sendMessage(message);
		}
	}

	private void initCamera(SurfaceHolder surfaceHolder){
		try{
			cameraManager.openDriver(surfaceHolder);
			
			// Creating the handler starts the preview, which can also throw a RuntimeException.
			if(handler == null){
				try{
					handler = new CaptureActivityHandler(this, decodeFormats, characterSet, cameraManager);
					//popup("[initCamera] new CaptureActivityHandler");
				} catch(Exception e){
					throw new Exception(e);
				}
			}
			decodeOrStoreSavedBitmap(null, null);
		} catch(IOException ioe){
			Log.w(TAG, ioe);
			// popup("[IO] " + ioe.getMessage());
			displayFrameworkBugMessageAndExit();
		} catch(RuntimeException e){
			// Barcode Scanner has seen crashes in the wild of this variety:
			// java.?lang.?RuntimeException: Fail to connect to camera service
			Log.w(TAG, "Unexpected error initializing camera", e);
			// popup("[RUN] " + e.getMessage());
			displayFrameworkBugMessageAndExit();
		} catch(Exception e){
			Log.w(TAG, "Unexpected error initializing camera", e);
			// popup("[Exception] " + e.getMessage());
			displayFrameworkBugMessageAndExit();
		}
	}
	
	private void displayFrameworkBugMessageAndExit(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.app_name));
		builder.setMessage(getString(R.string.msg_camera_framework_bug));
		builder.setPositiveButton(R.string.button_ok, new FinishListener(this));
		builder.setOnCancelListener(new FinishListener(this));
		builder.show();
	}
	
	public void restartPreviewAfterDelay(long delayMS){
		if(handler != null){
			handler.sendEmptyMessageDelayed(R.id.restart_preview, delayMS);
		}
		resetStatusView();
	}

	private void resetStatusView(){
		resultView.setVisibility(View.GONE);
		//statusView.setText(R.string.msg_default_status);
		statusView.setText(R.string.status_qr_scan);
		statusView.setVisibility(View.VISIBLE);
		viewfinderView.setVisibility(View.VISIBLE);
		lastResult = null;
	}

	public void drawViewfinder(){
		viewfinderView.drawViewfinder();
	}
	
	@Override
	public void onBackPressed(){
		backToDetailPage();
		return;
	}
	
	private void alertResult(String msg){
		final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
		alertbox.setMessage(msg);
		alertbox.setPositiveButton("OK", new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dlgInf, int which){
				backToDetailPage();
			}
		});
		alertbox.show();
	}
	private final void backToDetailPage(){
		Intent detailInt = null;
		if("MyCardDetail".equalsIgnoreCase(pageSrc)){
			detailInt = new Intent(this, MyCardDetail.class);
		} else if("MyCardRewardDetail".equalsIgnoreCase(pageSrc)){
			detailInt = new Intent(this, MyCardDetail.class);
			//detailInt = new Intent(this, MyCardRewardDetail.class);
			//detailInt.putExtra("promotionId", promotionId);
			//detailInt.putExtra("rewardNum", rewardNum);
		} else if("MyOfferDetail".equalsIgnoreCase(pageSrc)){
			detailInt = new Intent(this, MyOfferDetail.class);
		} else if("MyRewardDetail".equalsIgnoreCase(pageSrc)){
			detailInt = new Intent(this, MyRewardDetail.class);
		} else{
			detailInt = new Intent(this, Takepoints.class);
		}
		detailInt.putExtra("cardId", String.valueOf(cardId));
		startActivity(detailInt);
		finish();
    }
	
	// ========================== Take Now ========================
	class TakeNowProcess extends AsyncTask<String, Void, TakeReturn>{
		
		private Context context = null;
		private ProgressDialog progressDlg = null;
		
		TakeNowProcess(Context context){
			this.context = context;
		}
		
		@Override
        protected void onPreExecute(){
            super.onPreExecute();
            progressDlg = ProgressDialog.show(context, "Takepoints.com", "Taking ...", true);
		}
		
		@Override
        protected TakeReturn doInBackground(String... params){
			//String encryptedCode = params[0];
			String nfcId = params[0];
			
			TakeReturn tr = new TakeReturn();
			tr.setPromotionSize(-1);
			try{
				List<Promotion> promoL = PublicServices.getSitePromoListByNfcId(nfcId);
				if(promoL != null){
					if(promoL.size() == 1){
						tr = PublicServices.takeNow( getSharedIntValue(TPConst.PRF_MEMBER_ID)
												   , promoL.get(0).getPromotionId()
												   , nfcId );
					}
					tr.setPromotionSize(promoL.size());
				} else{
					tr.setPromotionSize(0);
				}
				tr.setNfcId(nfcId);
			} catch(Exception e){
				tr.setMessage(e.getMessage());
			}
			return tr;
		}
		
		@Override
        protected void onPostExecute(TakeReturn tr){
        	progressDlg.dismiss();
        	if(tr != null && !StringUtil.isEmpty(tr.getTrxCode())){
        		alertResult( "กรุณาแสดงรหัสนี้แก่พนักงาน : " + tr.getTrxCode()
       				 	 + (!StringUtil.isEmpty(tr.getTrxDesc())?"\n\n(" + tr.getTrxDesc() + ")":"") );
        		
			} else if(tr != null && tr.getPromotionSize() > 1){
				goToPromotionList(tr.getNfcId());
				
			} else if(tr != null && !StringUtil.isEmpty(tr.getMessage())){
				alertResult(tr.getMessage());
				
			} else{
				alertResult("ไม่พบโปรโมชั่น ณ ร้านค้านี้ !");
			}
		}
		
	}
	private void goToPromotionList(String nfcId){
		Intent intent = new Intent(this, TPPromotions.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra("nfcId", nfcId);
		startActivity(intent);
    }
	
}
